<?php

namespace app\api\controller;

use app\api\service\Token as TokenService;

use app\admin\model\Member;
use app\admin\model\User;

use think\Controller;
use think\Request;

class BaseController extends Controller
{

    const MSG_SUCCESS = '请求成功';
    const MSG_ERROR = '请求失败';
    const NOT_DATA = '暂无数据';
    const NOT_PARAM = '缺少参数';

    // 默认组织名
    protected $defaultOrganization = '智慧乡镇';


    protected function checkPrimaryScope(){
        TokenService::needPrimaryScope();

    }

    protected function checkExclusiveScope(){
        TokenService::needExclusiveScope();
    }

    public function getHttpParam()
    {
        $param = json_encode(Request::instance()->param());
        return json_decode($param);
    }

    /**
     * 根据镇ID 找到上级ID
     */
    public function getParentIdsByTownId($townId)
    {
        if(!$townId){
            return [];
        }
        $model = db('area');
        $townParentId = $model->where(['id' => $townId])->value('parentId');
        $xian = $model->where(['id' => $townParentId])->field('id, parentId')->find();
        $cityId = $model->where(['id' => $xian['parentId']])->value('id');

        $data = [
            'cityId' => $cityId,
            'xianId' => $xian['id'],
        ];

        return $data;
    }

    /**
     * 根据镇ID 得到配置
     */
    public function getTownConfig($townId)
    {
        if(!$townId){
            return [];
        }
        $model = db('townconfig');
        $config = $model->where(['townId' => $townId])->find();

        return $config;
    }

    /**
     * 获取用户信息
     */
    protected function getUserInfo($where){
        if(!$where['id']){
            return false;
        }
        $userInfo = User::where($where)->find();

        $memberId = $userInfo->memberId;
        if($memberId && $memberId !== 0){
            $userInfo = User::alias('a')->where(['a.id' => $where['id']])->join('__MEMBER__ m', 'a.memberId = m.id')->find();

            return $userInfo;
        }

        return $userInfo;
    }

    /**
     * 获取用户信息
     */
    protected function getMemberInfo($member){
        if(!$member){
            return '';
        }

        if ($member->villageId) {
            $villageName = getVillageName($member->villageId);
            $member['villageName'] = $villageName ? $villageName : '';
        }

        return $member;
    }

    /**
     * 七牛 图片裁剪
     */
    public function getCuttingImg($imgUrl, $config = [], $width = 330, $height = 220)
    {
        if(!$imgUrl){
            return $imgUrl;
        }

        if ($config) {
            // 布局类型
            $layoutType = $config['layoutType'];
            // 形状
            $shape = $config['shape'];
            switch ($layoutType) {
                // 列表布局
                case 1:
                    if($shape == 1){
                        // 长型
                        $width = 230;
                        $height = 153;
                    }else{
                        // 方型
                        $width = 160;
                        $height = 160;
                    }
                    break;
                // 方格布局
                case 2:
                    if($shape == 1){
                        // 长型
                        $width = 315;
                        $height = 210;
                    }else{
                        // 方型
                        $width = 240;
                        $height = 240;
                    }
                    break;
                // 滑动布局
                case 3:
                    if($shape == 1){
                        // 长型
                        $width = 330;
                        $height = 220;
                    }else{
                        // 方型
                        $width = 330;
                        $height = 330;
                    }
                    break;
                default: break;
            }
        }
        $cutImgUrl = $imgUrl . "?imageView2/1/w/{$width}/h/{$height}";

        return $cutImgUrl;
    }

}